package drds.data_propagate.binlog;

import drds.data_propagate.binlog.event.FormatDescriptionEvent;
import drds.data_propagate.binlog.event.GtidEvent;
import drds.data_propagate.binlog.event.TableMapEvent;
import drds.data_propagate.driver.packets.GtidSet;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO: Document Me!! NOTE: Log context will NOT write multi-threaded.
 */
public final class BinLogContext {

    private final Map<Long, TableMapEvent> tableIdToTableMapEventMap = new HashMap<Long, TableMapEvent>();

    private FormatDescriptionEvent formatDescriptionEvent;

    private BinlogPosition binlogPosition;

    private GtidSet gtidSet;

    private GtidEvent gtidEvent; // save current gtid log event

    public BinLogContext() {
        this.formatDescriptionEvent = FormatDescriptionEvent.FORMAT_DESCRIPTION_EVENT_5_x;
    }

    public BinLogContext(FormatDescriptionEvent descriptionEvent) {
        this.formatDescriptionEvent = descriptionEvent;
    }

    public final BinlogPosition getBinlogPosition() {
        return binlogPosition;
    }

    public final void setBinlogPosition(BinlogPosition binlogPosition) {
        this.binlogPosition = binlogPosition;
    }

    public final FormatDescriptionEvent getFormatDescriptionEvent() {
        return formatDescriptionEvent;
    }

    public final void setFormatDescriptionEvent(FormatDescriptionEvent formatDescriptionEvent) {
        this.formatDescriptionEvent = formatDescriptionEvent;
    }

    public final void putTable(TableMapEvent tableMapEvent) {
        tableIdToTableMapEventMap.put(Long.valueOf(tableMapEvent.getTableId()), tableMapEvent);
    }

    public final TableMapEvent getTable(final long tableId) {
        return tableIdToTableMapEventMap.get(Long.valueOf(tableId));
    }

    public final void clearAllTables() {
        tableIdToTableMapEventMap.clear();
    }

    public void reset() {
        formatDescriptionEvent = FormatDescriptionEvent.FORMAT_DESCRIPTION_EVENT_5_x;
        tableIdToTableMapEventMap.clear();
    }

    public GtidSet getGtidSet() {
        return gtidSet;
    }

    public void setGtidSet(GtidSet gtidSet) {
        this.gtidSet = gtidSet;
    }

    public GtidEvent getGtidEvent() {
        return gtidEvent;
    }

    public void setGtidEvent(GtidEvent gtidEvent) {
        this.gtidEvent = gtidEvent;
    }
}
